﻿#region 版权信息

// ------------------------------------------------------------------------------
// Copyright: (c) 2019  梅军章
// 项目名称：Amm.UnitOfWork
// 文件名称：IEnumerablePagedListExtensions.cs
// 版本号: V1.0.0.0
// 创建时间：2019-07-31 10:42
// 更改时间：2019-07-31 11:26
// ------------------------------------------------------------------------------

#endregion

#region 项目引用

using System;
using System.Collections.Generic;

#endregion

namespace Amm.UnitOfWork.Collections
{
    /// <summary>
    ///     Provides some extension methods for <see cref="IEnumerable{T}" /> to provide paging capability.
    /// </summary>
    public static class EnumerablePagedListExtensions
    {
        /// <summary>
        ///     Converts the specified source to <see cref="IPagedList{T}" /> by the specified <paramref name="pageIndex" /> and
        ///     <paramref name="pageSize" />.
        /// </summary>
        /// <typeparam name="T">The type of the source.</typeparam>
        /// <param name="source">The source to paging.</param>
        /// <param name="pageIndex">The index of the page.</param>
        /// <param name="pageSize">The size of the page.</param>
        /// <param name="indexFrom">The start index value.</param>
        /// <returns>An instance of the inherited from <see cref="IPagedList{T}" /> interface.</returns>
        public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize,
            int indexFrom = 0)
        {
            return new PagedList<T>(source, pageIndex, pageSize, indexFrom);
        }

        /// <summary>
        ///     Converts the specified source to <see cref="IPagedList{T}" /> by the specified <paramref name="converter" />,
        ///     <paramref name="pageIndex" /> and <paramref name="pageSize" />
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TResult">The type of the result</typeparam>
        /// <param name="source">The source to convert.</param>
        /// <param name="converter">The converter to change the <typeparamref name="TSource" /> to <typeparamref name="TResult" />.</param>
        /// <param name="pageIndex">The page index.</param>
        /// <param name="pageSize">The page size.</param>
        /// <param name="indexFrom">The start index value.</param>
        /// <returns>An instance of the inherited from <see cref="IPagedList{T}" /> interface.</returns>
        public static IPagedList<TResult> ToPagedList<TSource, TResult>(this IEnumerable<TSource> source,
            Func<IEnumerable<TSource>, IEnumerable<TResult>> converter, int pageIndex, int pageSize, int indexFrom = 0)
        {
            return new PagedList<TSource, TResult>(source, converter, pageIndex, pageSize, indexFrom);
        }
    }
}